#ifndef SDK_INTERCOM_MANAGER_H_
#define SDK_INTERCOM_MANAGER_H_

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "intercom_message.h"
#import "net_client.h"
#import "net_device.h"
#import "intercom_constants.h"
#import "button_message.h"
#import "smarthome_message.h"

@class IntercomAppDelegateHolder;
@class IntercomConversationDelegateHolder;
@class IntercomProxyDelegateHolder;
@class IntercomSmartHomeDelegateHolder;

@class SmartHomeManager;
@class IntercomClientManager;
@class IntercomSessionManager;
@class CaptureConfig;

/*
 * 门禁对象，可以有多个，如果有多个家庭的话
 * 每个对象关联一个family ID
 * 不允许直接创建此对方，必须通过IntercomManager addIntercom
 * 也不允许直接销毁，必须通过IntercomManager removeIntercom
 * remove record property
 */
@interface Intercom : NSObject

// 0:有室内机的类型，1:表示纯云方案，无室内机
@property(nonatomic, assign, readonly) int intercom_type;
// family id
@property(nonatomic, strong, readonly) NSString *fid;

//本机client的信息，每个家庭不一样的，但clientid允许相同
@property(nonatomic, readonly) NetClient *netClient;

//此家庭中所有终端和代理的信息
@property(nonatomic, strong) IntercomClientManager *clientManager;

//此家庭中当前活跃会话
@property(nonatomic, strong) IntercomSessionManager *sessionManager;

//此家庭智能家居管理
@property(nonatomic, strong) SmartHomeManager *smarthome;

@property(nonatomic, strong, readonly)
    IntercomProxyDelegateHolder *proxy_delegate;

@property(nonatomic, strong, readonly)
    IntercomConversationDelegateHolder *intercom_delegate;

@property(nonatomic, strong, readonly)
    IntercomSmartHomeDelegateHolder *smarthome_delegate;

//启动代理
- (BOOL)startProxy:(NSString *)prox_conf;

//停止代理
- (void)stopProxy;

//启动门禁系统
- (BOOL)startIntercom:(NSString *)server_conf;

//停止门禁系统
- (void)stopIntercom;

//向这个Intercom对象发消息，如果消息中包含content，需要base64
- (BOOL)sendIntercomMessage:(NSDictionary *)bundle;

//向这个Intercom对象发命令
- (BOOL)sendIntercomCommand:(NSDictionary *)bundle;

- (BOOL)sendIntercomConfigure:(Router)router
                   net_client:(NetClient *)net_client
                           to:(NSString *)to
                   session_id:(NSString *)session_id
                       config:(NSString *)config;

//更新某个会话的视频显示图层，当需要更换图层的时候，再次调用此方法通知底层
- (BOOL)updateSurface:(NSString *)session_id layer:(CALayer *)layer;

//向这个家庭其他设备发消息
- (BOOL)
sendFamilyMessage:(Router)router
       net_client:(NetClient *)net_client  //通过哪个代理
               to:(NSString *)to           //对方的client_id
       session_id:(NSString *)session_id   // optional
          content:(NSString *)content  //消息正文，会被打包到InercomMessage中
              ack:(BOOL)ack;

@end

/*
 * 门禁系统的管理,在初始化之前，至少要通过IntercomAppDelegateHolder 添加
 * IntercomAppDelegate代理
 */
@interface IntercomManager : NSObject

@property(nonatomic, strong, readonly) IntercomAppDelegateHolder *app_delegate;

//全局初始化，只需要调用一次，必须在调用其他任何方法之前初始化
+ (BOOL)globalInitialize:(NSString *)app_conf;

//此刻已经创建了工作线程，IntercomManager应该在app层面创建
//对移动端来说，永远不要销毁
- (id)init:(NSString *)port_conf;

//停止工作线程，销毁所有native对象
- (void)unInit;

/*
 * 启动门禁系统,创建native对象,if changed account,stop first then start again
 * DONOT call init /unInit,you never call it twice!
 * system_id:系统唯一标示
 * media_conf:media 配置(MediaConfig)
 */
- (BOOL)start:(NSString *)system_id
              media_conf:(NSString *)media_conf
    use_external_encoder:(BOOL)use_external_encoder;

/*
 * 停止门禁系统,如果需要切换账号的话，先stop，再重新start即可
 */
- (void)stop;

- (void)active:(NSString *)appid
        appkey:(NSString *)appkey
    activecode:(NSString *)activecode;

/*
 * 添加一个家庭的门禁系统
 * client_conf: 终端配置(ClientConf)
 * intercom_type:0:has indoor, 1: cloud
 */
- (Intercom *)addIntercom:(NSString *)fid
              client_conf:(NSString *)client_conf
            intercom_type:(int)intercom_type;

/**
 *删除一个家庭的门禁系统
 */
- (void)removeIntercom:(NSString *)fid;

/**
 *根据家庭ID查询对应的门禁系统
 */
- (Intercom *)getIntercom:(NSString *)fid;

/**
 *更新摄像头预览显示图层
 */
- (BOOL)updatePreviewSurface:(CALayer *)layer;


/** 从推送服务器收到的消息调用此方法传给底层处理 */
- (BOOL)sendPushMessage:(NSString*)message;

/** 系统级的命令，playback控制通过此接口*/
- (BOOL)sendAppCommand:(NSDictionary*)message;

/** 切换摄像头：kFrontCameraName:前置 kBackCameraName:后置 */
- (BOOL)switchCamera:(CaptureConfig*)conf;

- (void)onExternalVideoFrameAvailable:(int)format
                      width:(int)width
                     height:(int)height
                       data:(const char*)data
                     length:(int)length
                  timestamp:(long long)timestamp;

// get license info,return json
+ (NSString *)getActiveInfo;

/** 查询当前系统中保存的录像名称列表 */
+ (NSArray<NSString *> *)enumeratorRecord:(NSString*)media_dir;

/** 当前sdk版本*/
+ (NSString *)getSDKVersion;

@end

#endif //SDK_INTERCOM_MANAGER_H_
